<?php

namespace App\Http\Controllers\Admin\Adseat;

use App\Area;
use App\Category;
use App\Post;
use App\Service\Adseat;
use App\Service\WeChat;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdseatController extends Controller
{
    private $adseatManager;
    private $request;
    public function __construct (Request $request)
    {
        $this->adseatManager = new Adseat();
        $this->request = $request;
    }

    /**
     * 文章列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{
            return view('admin.adseat.adseat.index',[
                'status' =>    $this->adseatManager->getStatusList(),
                'adseatManager'   =>  $this->adseatManager
            ]);
        }

    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(){
        return $this->page_json_render($this->adseatManager->getPageList($this->request));
    }


    /**
     * 编辑页面
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id){
        $model = $this->adseatManager->where('id',$id)->first();
        return view('admin.adseat.adseat.add-edit',[
            'status' =>    $this->adseatManager->getStatusList(),
            'adseatManager'   =>  $this->adseatManager,
            'id'    =>  $id,
            'model' =>  $model
        ]);
    }


    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(){
        return view('admin.adseat.adseat.add-edit',[
            'status' =>    $this->adseatManager->getStatusList(),
            'adseatManager'   =>  $this->adseatManager,
        ]);
    }

    /**
     * 保存或插入
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveOrInsert(){
        //dd($this->request->post());
        $this->validate($this->request,[
            'title'    =>  'required|max:50',
            'digest'    =>  'max:255',
            'link'   =>  'required',
            'img' =>  'required',
        ]);
        $this->adseatManager->insertOrUpdate($this->request->post());
        return redirect()->route('admin.adseat')->with(
            $this->success()
        );
    }

    /**
     * @return array
     */
    private function draft(){
        try{
            $this->adseatManager->draft($this->request->id);
            return  ['info'  =>  'success','msg'    =>  $this->request->id];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }

    /**
     * @return array
     */
    private function published(){
        try{
            $this->adseatManager->published($this->request->id);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }
    /**
     * @return array
     */
    private function delete(){
        try{
            $this->adseatManager->del($this->request->id);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }

    private function sortCol(){
        try{
            $this->adseatManager->sortCol($this->request->id,$this->request->sort);
            return  ['info'  =>  'success'];
        }catch (\Exception $e){
            return ['info' =>  'error'];
        }
    }
}
